/********************************************************************************


 **** Copyright (C), 2024, Yuanlong Xu <Yono233@outlook.com>    ****
 **** All rights reserved                                       ****

 ********************************************************************************
 * File Name     : MBx_utility_MemMove.c
 * Author        : yono
 * Date          : 2024-07-23
 * Version       : 1.0
********************************************************************************/
/**************************************************************************/
/*
    modbusX 内存拷贝工具
*/

/* Includes ------------------------------------------------------------------*/
#include <MBx_api.h>
/* Private types -------------------------------------------------------------*/
/* Private macros ------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private Constants ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/**
 * @brief 移动内存块
 * @param dest 目标内存地址
 * @param src 源内存地址
 * @param n 要移动的字节数
 */
void MBx_utility_MemMove(void *dest, const void *src, size_t n)
{
    uint8_t       *d = dest;
    const uint8_t *s = src;

    if(d < s)
    {
        // 从前往后拷贝
        while(n--)
        {
            *d++ = *s++;
        }
    }
    else
    {
        // 从后往前拷贝
        d += n;
        s += n;
        while(n--)
        {
            *--d = *--s;
        }
    }
}